package com.vq3.digitalizationsoftwareintegrates.Service.User.Impl;

import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.Group_Type;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.StatusType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.*;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_WebSocketMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.S_C_DatesMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.S_C_PointMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.S_C_Point_ContentMapper;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import com.vq3.digitalizationsoftwareintegrates.Service.User.AuditService;
import com.vq3.digitalizationsoftwareintegrates.Utils.DateUtils.TaskUtils;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.CalculationIsType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class AuditServiceImpl implements AuditService {
    @Resource
    MongoDB_S_C_Point_ContentDao mongoDB_s_c_point_contentDao;
    @Resource
    S_C_PointMapper s_c_pointMapper;
    @Resource
    S_C_Point_ContentMapper s_c_point_contentMapper;
    @Resource
    S_C_DatesMapper s_c_datesMapper;

    public boolean S_C_AuditReviewNG(String itemName, StatusType statusType) {
        return mongoDB_s_c_point_contentDao.S_C_AuditReviewNG(itemName,statusType);
    }

    @Override
    public boolean S_C_AuditReviewNG(String itemName, StatusType statusType, List<String> cleaningRemarkContext) throws Exception {
        return mongoDB_s_c_point_contentDao.S_C_AuditReviewNG(itemName,statusType,cleaningRemarkContext);
    }

    public String itemNameFindUName(String itemName,StatusType statusType) {
        return mongoDB_s_c_point_contentDao.itemNameFindUName(itemName,statusType);
    }
    public MongoDB_S_C_WebSocketMessageEntity itemNameFindRemarkContext(String itemName, StatusType statusType){
        return mongoDB_s_c_point_contentDao.itemNameFindRemarkContext(itemName,statusType);
    }

    public boolean S_C_AuditPass(String itemName,String uname) throws EncodeException, IOException {
        boolean b = false;
        boolean b1 = mongoDB_s_c_point_contentDao.S_C_AuditPass(itemName, uname);
        MongoDB_S_C_WebSocketMessageEntity s_c_pass = null;
        if (b1) {
            s_c_pass = mongoDB_s_c_point_contentDao.findS_C_Pass(itemName, uname);
            try {
                b = S_C_PersistentStorage(s_c_pass);

            }catch (Exception e) {
                e.printStackTrace();
            }
        }


        return b;
    }
    /**持久层存储的方法,存点位名的表
     * */

    boolean S_C_PersistentStorage(MongoDB_S_C_WebSocketMessageEntity messageEntity) throws EncodeException, IOException {
        int i = 0;
        int insert = 0;
        int insert1 = 0;
        int insert2 = 0;

        String itemName = messageEntity.getItemName();
        List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntityS = messageEntity.getMongoDB_s_c_point_contentEntityS();
        S_C_PointEntity orderByIDLimit1 = s_c_pointMapper.findOrderByIDLimit1();
        System.out.println(mongoDB_s_c_point_contentEntityS);
        for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntityS) {


            i++;
            S_C_Point_ContentEntity s_c_point_contentEntity = new S_C_Point_ContentEntity();
            Group_Type group_type = mongoDB_s_c_point_contentEntity.getGroup_type();
            Integer integer = CalculationIsType.calculationIsGroupTypeToMySql(group_type);
            S_C_PointEntity s_c_pointEntity = new S_C_PointEntity();
            S_C_DatesEntity sCDatesEntity = new S_C_DatesEntity();

            try{
                s_c_pointEntity.setPointId(orderByIDLimit1.getPointId()+1);
            }catch (NullPointerException nullPointerException) {
                s_c_pointEntity.setPointId(1);
            }
            s_c_pointEntity.setPointName(itemName);
            s_c_pointEntity.setPointContentId(i);
            s_c_pointEntity.setGroupId(integer);

            try{
                s_c_pointEntity.setPointCount(orderByIDLimit1.getPointCount() + 1);
            }catch (NullPointerException nullPointerException) {
                nullPointerException.printStackTrace();
                s_c_pointEntity.setPointCount(1);
            }

            s_c_pointEntity.setCreateTime(new Date());
            s_c_pointEntity.setUpdateTime(new Date());
            sCDatesEntity.setDate(new Date());
            sCDatesEntity.setPoint(itemName);
            sCDatesEntity.setPointContent(mongoDB_s_c_point_contentEntity.getPoint_Content());
            s_c_pointEntity.setStatusType(mongoDB_s_c_point_contentEntity.getStatus_Type().toString());
            s_c_pointEntity.setDeleteStatus(1);
            s_c_point_contentEntity.setPointContent(mongoDB_s_c_point_contentEntity.getPoint_Content());
            s_c_point_contentEntity.setScFun(mongoDB_s_c_point_contentEntity.getSc_fun());
            s_c_point_contentEntity.setScBenchmark(mongoDB_s_c_point_contentEntity.getSc_benchmark());
            String imagePath = mongoDB_s_c_point_contentEntity.getImagePath();
            String imageName = mongoDB_s_c_point_contentEntity.getImageName();
            String imgPath = null;
            if (imageName != null){
                imgPath = imageName +".jpeg";
            }
            if (imagePath != null) {
                s_c_point_contentEntity.setPointContentStandard(imgPath);
            }
            s_c_point_contentEntity.setPointName(itemName);
            s_c_point_contentEntity.setScDeletestatus(1);



            insert1 += s_c_pointMapper.insert(s_c_pointEntity);
            insert += s_c_point_contentMapper.insert(s_c_point_contentEntity);
            insert2 += s_c_datesMapper.insert(sCDatesEntity);



        }
        return insert > 0 && insert1 > 0 && insert2 > 0;
    }
    /**持久层的存储，存点检信息
     * */

}
